<template>
	<view class="login">
		<view class="login_title">欢迎登录</view>
		<view class="login_form">
			<view class="login_form_item">
				<input class="login_form_item_input" v-model="username" focus placeholder="请输入手机号码"
					placeholder-style="font-size:24upx;color:#b1b1b1;" />
			</view>
			<view class="login_form_item">
				<input type="password" v-model="password" class="login_form_item_input" placeholder="请输入密码"
					placeholder-style="font-size:24upx;color:#b1b1b1;" />
			</view>
		</view>
		<view class="login_btn">
			<button type="primary" @click="doLogin">登录</button>
		</view>
		<view class="login_toPage">
			<view>忘记密码？</view>
			<view @click="toRegister">立即注册</view>
		</view>
	</view>
</template>

<script setup>
	//13552469502
	//123
	import {
		ref
	} from 'vue';
	import {
		login,
		loginByopenid
	} from '../../api/user.js'
	import {
		useStore
	} from 'vuex' // 引入useStore 方法
	const store = useStore();
	let username = ref('');
	let password = ref('');
	// let openid = ref('');
	const toRegister = () => {
		uni.navigateTo({
			url: '/pages/register/register'
		})
	}
	const loginRes = () => {
		console.log("hahah ")
		return new Promise((resolve, reject) => {
			uni.login({
				timeout: 10000,
				provider: 'weixin',
				success: (res) => {
					//登陆成功的回调
					resolve(res)
				},
				fail: (err) => {
					//登陆失败的回调
					reject(err)
				}
			})
		})
	}
	const doLogin = () => {
		let pattern = /^[\s\S]*.*[^\s][\s\S]*$/;
		let isPhoneNumber = /^1(3\d|4[5-9]|5[0-35-9]|6[567]|7[0-8]|8\d|9[0-35-9])\d{8}$/
		if (!pattern.test(username.value)) {
			uni.showToast({
				icon: 'error',
				title: '用户名不能为空'
			})
			return
		}
		if (!isPhoneNumber.test(username.value)) {
			uni.showToast({
				icon: 'error',
				title: '手机号码格式错误'
			})
			return
		}
		if (!pattern.test(password.value)) {
			uni.showToast({
				icon: 'error',
				title: '密码不能为空'
			})
			return
		}
		console.log("9999999")
		loginRes().then((res) => {
			console.log("11111111111111111111")
			if (res.errMsg == "login:ok") {
				let code = res.code;
				loginByopenid({
					code: code
				}).then((res) => {
					console.log(res);
					let openid = res.data.openid;
					let data = {
						username: username.value,
						password: password.value,
						openid: openid
					}
					store.dispatch('user/login', data).then(res => {
						store.commit('user/submitUserInfo', res)
						uni.showToast({
							icon: 'success',
							title: '登录成功',
							duration: 1000
						})
						setTimeout(() => {
							uni.switchTab({
								url: '/pages/index/index'
							})
						}, 1000)
					})

				})
			} else {
				uni.showToast({
					icon: 'error',
					title: res.errMsg
				})
			}

		})

		// loginRes().then(res => {
		// 	console.log(res)
		// 	let {
		// 		code
		// 	} = res;
		// 	return getOpenId(code)
		// }).then(res => {
		// 	console.log(res)
		// 	let {
		// 		data: {
		// 			openid,
		// 			session_key
		// 		}
		// 	} = res;
		// 	store.dispatch('openIdAction', openid);
		// 	console.log(openid)



	}
</script>

<style lang="scss">
	.login {
		padding: 20px;
		overflow: hidden;

		&_title {
			margin-top: 100upx;
			;
			font-size: 48upx;
			font-weight: bold;
		}

		&_form {
			margin-top: 60upx;

			&_item {
				margin-bottom: 30upx;

				&_input {
					border: 1px solid #e1e1e1;
					height: 80upx;
					border-radius: 5upx;
					padding-left: 10upx;
					background: #fff;
				}
			}
		}

		&_btn {
			margin-top: 100upx;

			button[type=primary] {
				background-color: #17adf0;
			}
		}

		&_toPage {
			padding-top: 20upx;
			display: flex;
			justify-content: space-between;
			font-size: 24upx;
			color: #b1b1b1;
		}
	}
</style>